package com.gitee.dbswitch.admin.controller.converter;

import cn.hutool.extra.spring.SpringUtil;
import com.gitee.dbswitch.admin.dao.APIAssignmentConfigDAO;
import com.gitee.dbswitch.admin.dao.AssignmentConfigDAO;
import com.gitee.dbswitch.admin.dao.AssignmentJobDAO;
import com.gitee.dbswitch.admin.dao.DatabaseConnectionDAO;
import com.gitee.dbswitch.admin.entity.*;
import com.gitee.dbswitch.admin.model.response.APIAssignmentInfoResponse;
import com.gitee.dbswitch.admin.model.response.AssignmentInfoResponse;
import com.gitee.dbswitch.admin.type.JobStatusEnum;
import com.gitee.dbswitch.common.converter.AbstractConverter;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * api 实体类映射
 *
 * @author natural
 */
public class APIAssignmentInfoConverter extends
        AbstractConverter<AssignmentTaskEntity, APIAssignmentInfoResponse> {

    private final static String UNKNOWN_CONNECTION_TYPE = "Unknown";
    private APIAssignmentConfigDAO apiAssignmentConfigDAO = SpringUtil.getBean(APIAssignmentConfigDAO.class);
    private DatabaseConnectionDAO databaseConnectionDAO = SpringUtil.getBean(DatabaseConnectionDAO.class);
    private AssignmentJobDAO assignmentJobDAO = SpringUtil.getBean(AssignmentJobDAO.class);


    @Override
    public APIAssignmentInfoResponse convert(AssignmentTaskEntity assignmentTaskEntity) {
        Long taskId = assignmentTaskEntity.getId();
        APIAssignmentConfigEntity apiAssignmentConfigEntity = apiAssignmentConfigDAO.getByAssignmentTaskId(taskId);
        Long targetConnectionId = apiAssignmentConfigEntity.getTargetConnectionId();
        List<Long> connectionIds = Lists.newArrayList(targetConnectionId);
        Map<Long, DatabaseConnectionEntity> databaseConnectionMap = databaseConnectionDAO.getByIds(connectionIds)
                .stream().collect(Collectors.toMap(DatabaseConnectionEntity::getId, Function.identity()));

        APIAssignmentInfoResponse response = new APIAssignmentInfoResponse();
        response.setId(assignmentTaskEntity.getId());
        response.setName(assignmentTaskEntity.getName());
        response.setDescription(assignmentTaskEntity.getDescription());
        response.setScheduleMode(assignmentTaskEntity.getScheduleMode());
        response.setCronExpression(assignmentTaskEntity.getCronExpression());
        response.setIsPublished(assignmentTaskEntity.getPublished());
        response.setCreateTime(assignmentTaskEntity.getCreateTime());
        response.setUpdateTime(assignmentTaskEntity.getUpdateTime());
        response.setApiName(apiAssignmentConfigEntity.getApiName());
        response.setTargetSchema(apiAssignmentConfigEntity.getTargetSchema());


        if (databaseConnectionMap.containsKey(targetConnectionId)) {
            response.setTargetType(databaseConnectionMap.get(targetConnectionId).getType().getName());
        } else {
            response.setTargetType(UNKNOWN_CONNECTION_TYPE);
        }

        AssignmentJobEntity assignmentJobEntity = assignmentJobDAO.getLatestJobEntity(taskId);
        Integer status = (assignmentJobEntity == null || assignmentJobEntity.getStatus() == null) ?
                JobStatusEnum.INIT.getValue() :
                assignmentJobEntity.getStatus();
        response.setRunStatus(JobStatusEnum.of(status).getName());
        response.setScheduleTime(assignmentJobEntity == null ? null : assignmentJobEntity.getStartTime());

        return response;
    }
}
